const app = getApp()
const innerAudioContext = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    changeShow:false,
    actions: [
      { name: '工单转出',id:0},
      { name: '服务完成',id:1},
    ],
    money:'',
    change_id:0,
    change_engineer:'',
    solve_time:'',
    minDate: new Date().getTime(),
    soundState: 0,
    order: {},
    steps: []
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    var param;
    app.getParam(options, (rel) => {
      param = rel;
      this.setData({
        id: rel.id,
      })
    })
  },
  onShow: function () {
    this.getApi(this.data.id);
  }, 
  //获取API判断是否工程师
  getApi: function (id) {
    var parms = {
      signkey: 'engineer'
    }
    app.api().Get('api/v1/bestbao/engineer/isEngineer', parms, (result) => {
      if (result.data = 1){
        this.setData({
          engineer: result.data
        })
        this.getOrder(id);
      }else{
        app.wxAlert('非工程师禁止访问本页面');
      }
    })
  },
  //获取API
  getOrder: function (id) {
    var parms = {
      id: id,
    }
    app.api().Get('api/v1/bestbao/engineer/reView', parms, (result) => {
      if (result.code == 200) {
        this.setData({
          order: result.data.order,
          steps: result.data.process,
          active: result.data.process.length - 1
        });
        if (!app.util().isNull(result.data.order.imgs)){
          setTimeout(() => {
            this.getWidghHeight();
          }, 1000)
        }
      }
    })
  },
  //接单
  doOrder:function() {
    if (app.util().isNull(this.data.solve_time)){
      app.wxAlert('预计服务时间必须填写');
    }else{
      var parms = {
        id: this.data.id,
        solve_time: this.data.solve_time,
      }
      app.api().Post('api/v1/bestbao/engineer/doOrder', parms, (result) => {
        this.onOrder();
        if (result.code == 200) {
          app.wxAlert(result.msg, () => {
            wx.navigateBack({
              delta: 1
            })
          })
        }
      })
    }
  },
  //人员流转
  changeOrder:function(){
    if (app.util().isNull(this.data.change_engineer) || this.data.change_id == 0) {
      app.wxAlert('必须选择转出人员');
    } else {
      var parms = {
        id: this.data.id,
        change_id: this.data.change_id,
        change_engineer: this.data.change_engineer,
      }
      app.api().Post('api/v1/bestbao/engineer/changeOrder',parms,(result) => {
        this.onPopupShow();
        if (result.code == 200) {
          app.wxAlert(result.msg, () => {
            wx.navigateBack()
          })
        }
      })
    }
  },
  //图片预览
  previewImage: function (e) {
    wx.previewImage({
      current: e.target.dataset.src,
      urls: this.data.order.imgs
    })
  },
  //电话预约
  onCall: function (e) {
    app.callPhone(e);
  },
  //弹窗
  onOrder: function() {
    this.setData({
      orderShow: !this.data.orderShow
    });
  },
  onSheet: function () {
    this.setData({
      sheet: !this.data.sheet
    });
  },
  onSelect: function (e) {
    if (e.detail.id == 1){
      wx.navigateTo({
        url: 'endOrder?id=' + this.data.order.id,
      })
    }else{
      wx.navigateTo({
        url: 'card',
      })
      this.onPopupShow();
    }
  },
  onClose: function () {
    this.setData({
      show: !this.data.show
    });
  },
  onPopupShow: function () {
    this.setData({
      changeShow: !this.data.changeShow
    });
  },
  onPopupEndShow: function () {
    this.setData({
      endShow: !this.data.endShow
    });
  },
  //支付费用
  onChangeMoney: function (event) {
    this.setData({
      money: event.detail
    })
  },
  //去选择工程师
  onChange: function () {
    wx.navigateTo({
      url: 'card',
    })
  },
  //播放语音
  playSound: function (e) {
    let soundState = this.data.soundState;
    switch (soundState) {
      case 1: //暂定播放
        this.setData({
          soundState: 0
        })
        this.stopPlay();
        break;
      default: //重新开始
        this.setData({
          soundState: 1,
          disabled: true
        })
        this.play();
    }
  },
  //播放语音
  play: function (e) {
    if (!app.util().isNull(this.data.order.sound)) {
      innerAudioContext.autoplay = true
      innerAudioContext.src = this.data.order.sound,
        innerAudioContext.play()
      innerAudioContext.onEnded((res) => {
        this.setData({
          soundState: 0
        })
        this.stopPlay();
      })
    }
  },
  //暂定播放
  stopPlay: function () {
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempSoundPath,
    innerAudioContext.stop(() => {
      console.log('暂定播放')
    })
  },
  //时间  
  onDatetime: function (event) {
    var times = event.detail;
    var currentDate = app.util().timesToDate(times, 'Y-M-D h:m');
    this.setData({
      solve_time: currentDate
    })
    this.onClose();
  },
  /**
  * 获取图片宽高
  */
  getWidghHeight: function () {
    let that = this;
    const query = wx.createSelectorQuery()
    query.select('.select_img').boundingClientRect(function (res) {
      that.setData({
        width: parseInt(res.width)
      })
    }).exec();
  },
})